package doantotnghiep.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.PurchaseOrdeDao;
import doantotnghiep.model.Customer;
import doantotnghiep.model.PurchaseOrder;
import doantotnghiep.model.PurchaseOrderDetail;

public class PurchaseOrderDaoImpl implements PurchaseOrdeDao {


	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Override
	public synchronized void addPurchase(PurchaseOrder purchaseOrder) {
		this.hibernateTemplate.saveOrUpdate(purchaseOrder);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> getAllCustomer() {
		return hibernateTemplate.find("from Customer");		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseOrder> getAllPurchase() {
		return hibernateTemplate.find("from PurchaseOrder");		
	}

	@Override
	public void deletePurchase(int id) {
		PurchaseOrder purchaseOrder = this.getPurchaseByID(id);
		if(purchaseOrder != null){
			hibernateTemplate.delete(purchaseOrder);
		}
	}

	@SuppressWarnings("unchecked")
	private PurchaseOrder getPurchaseByID(int id) {
		List<PurchaseOrder> purchaseOrders = hibernateTemplate.find("from PurchaseOrder where id ="+ id);
		if(purchaseOrders!=null){
			return purchaseOrders.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getCurrentPurchaseID() {
		List<PurchaseOrder> resultSet = hibernateTemplate.findByNamedQuery("getCurentPurchase");
		if(resultSet!=null){
			PurchaseOrder purchase = (PurchaseOrder) resultSet.get(0);
			int id = purchase.getOrderID();
			return id;	
		}
		return 0;	
	}

	@Override
	public void addOrderDetail(PurchaseOrderDetail purchaseOrderDetail) {
		this.hibernateTemplate.saveOrUpdate(purchaseOrderDetail);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseOrderDetail> getAllOrderDetailByID(int id) {
		List<PurchaseOrderDetail> details = this.hibernateTemplate.find("from PurchaseOrderDetail where MaDonDH = "+id);
		return details;
	}

	@Override
	public void deleteOrderDetail(int id) {
		PurchaseOrderDetail detail = this.getOrderDetailByID(id);
		if(detail!=null){
			hibernateTemplate.delete(detail);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public PurchaseOrderDetail getOrderDetailByID(int id){
		List<PurchaseOrderDetail> list = hibernateTemplate.find("from PurchaseOrderDetail where id = "+id);
		if(list!=null){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<PurchaseOrderDetail> getListOrderDetailByID(int id) {
		List<PurchaseOrderDetail> list = hibernateTemplate.find("from PurchaseOrderDetail where MaNCC = "+id);
		if(list!=null){
			return (ArrayList<PurchaseOrderDetail>) list;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseOrder> getAllPurchaseNotExported(String date) {
		List<PurchaseOrder> list = hibernateTemplate.find("from PurchaseOrder where isExported = 0 and orderedDate ='"+date+"'");
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseOrder> getAllPurchaseByOrderDate(String orderDate) {
		List<PurchaseOrder> list = hibernateTemplate.find("from PurchaseOrder where orderedDate = '"+orderDate+"'");
		return list;
	}

}
